import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author: knowno
 * @create: 2025-09-18 09:04
 * @Version 1.0
 **/
public class TestJDBC4 {
    public static void main(String[] args) {

        try {

            //1 获取链接
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hisdb2", "root", "root");
            System.out.println(conn.getClass());
            //2 定义SQL语句
            String sql = "select * from role";
            //3 预编译SQL语句
            PreparedStatement pstmt = conn.prepareStatement(sql);
            System.out.println(pstmt);
            //4.执行SQL语句 =>查询的sql语句
            ResultSet rs = pstmt.executeQuery();
            //com.mysql.cj.jdbc.result.ResultSetImpl

            //5 处理结果集
            while(rs.next()){
                System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t"+rs.getString("comment"));
            }
            //6.关闭资源
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
